package com.wenjunwu.blog.common;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Aspect
public class ViewCountAspect {
	
	@Autowired
	private JedisPool jedisPool;
	
	@Before("execution(* com.wenjunwu.blog.service.impl.BlogContentServiceImpl.selectByPrimaryKey(..))")
	public void viewCount(JoinPoint point) {
		Integer viewCount = 1;
		String blogIdKey = String.valueOf(point.getArgs()[0]);
		Jedis jedis = jedisPool.getResource();
		if (StringUtils.isEmpty(jedis.get(blogIdKey))) {
			jedis.set(blogIdKey, viewCount.toString());
		} else {
			Integer vc = Integer.valueOf(jedis.get(blogIdKey))+1;
			jedis.set(blogIdKey,vc.toString());
		}
	}
}
